import { request } from "../../utils/request"
// 手指起始的坐标
let startY = 0
// 手指移动的坐标
let moveY = 0
//手指移动的距离
let moveDistance = 0
//获取全局实例
const appInstance = getApp()
let timer = null
Page({
  /**
   * 页面的初始数据
   */
  data: {
    coverTransform: "translateY(0rpx)",
    coverTransition: "",
    timeList: [
      { time: 0.3, info: "18秒" },
      { time: 20, info: "20分钟" },
      { time: 30, info: "30分钟" },
      { time: 60, info: "1小时" },
      { time: 90, info: "1小时30分钟" },
      { time: 120, info: "2小时" },
    ],
    currentData: "",
    // 用户信息
    userInfo: {},
    playHistory: [],
    isShow: false,
  },
  // 定时关闭歌曲选择时间

  timeChange(e) {
    //获取定时关闭时间
    const stopTime = this.data.timeList[e.detail.value].time * 60 * 1000
    //清除定时器
    clearTimeout(timer)
    //定时器
    timer = setTimeout(() => {
      if (appInstance.globalData.backgroundAudioManager == "") {
        return
      } else {
        appInstance.globalData.backgroundAudioManager.stop()
      }
    }, stopTime)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    let userInfo = wx.getStorageSync("userInfo")
    let playHistory = wx.getStorageSync("playHistory")
    if (userInfo) {
      // 更新userInfo的状态
      this.setData({
        userInfo: userInfo,
      })
    }
    if (playHistory.length) {
      this.setData({
        playHistory,
      })
    }
  },
  handleTouchStart(even) {
    this.setData({
      coverTransition: "",
    })
    startY = even.touches[0].clientY
  },
  handleTouchMove(even) {
    moveY = even.touches[0].clientY
    moveDistance = moveY - startY
    // console.log( moveDistance)
    if (moveDistance <= 0) {
      return
    }
    if (moveDistance >= 80) {
      moveDistance = 80
    }
    this.setData({
      coverTransform: `translateY(${moveDistance}rpx)`,
    })
  },
  handleTouchEnd() {
    this.setData({
      coverTransform: `translateY(0rpx)`,
      coverTransition: "transform 1s linear",
    })
  },
  login() {
    return new Promise((resolve, reject) => {
      wx.login({
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(err)
        },
      })
    })
  },
  async getUserInfo() {
    try {
      wx.showLoading({ title: "正在登录" })
      const getInfo = () => wx.getUserProfile({ desc: "获取信息" })
      const [info, loginData] = await Promise.all([getInfo(), this.login()])
      let code = loginData.code
      let userInfo = info.userInfo
      let userMessage = { code, ...userInfo }
      let res = await request("/user", "POST", { ...userMessage })
      const { token } = res.data
      wx.hideLoading()
      if (token) {
        wx.setStorageSync("token", token)
        wx.setStorageSync("userInfo", info.userInfo)
        wx.setStorageSync("isLogin", true)
        getApp().globalData.isLogin = true
        this.setData({
          userInfo: info.userInfo,
        })
        let startMapData = await request("/start/map")
        wx.setStorageSync("startMapList", startMapData.data)
      }
    } catch (error) {
      console.log(error)
    }
  },
  logout() {
    wx.removeStorageSync("userInfo")
    wx.removeStorageSync("token")
    wx.removeStorageSync("isLogin")
    getApp().isLogin = false
    this.setData({
      userInfo: "",
    })
  },
  toPersonalData() {
    wx.navigateTo({
      url: "/pages/personalData/personalData",
    })
  },
  toCollect() {
    const isLogin = wx.getStorageSync("isLogin")
    if (isLogin) {
      wx.navigateTo({
        url: "/pages/collect/collect",
      })
    } else {
      wx.showToast({ title: "请先登录", icon: "error", duration: 1000 })
    }
  },
  toAboutUs() {
    wx.navigateTo({
      url: "/pages/aboutUs/aboutUs",
    })
  },
  onClose() {
    this.setData({
      isShow: false,
    })
  },
  recommendPaly(e) {
    let index = e.currentTarget.dataset.index
    let currentMusic = this.data.playHistory[index]
    wx.navigateTo({
      url: "/pages/songDetail/songDetail",
      success: function (res) {
        res.eventChannel.emit("detail", { musicData: currentMusic })
      },
    })
  },
})
